//
//  sqliteCore.m
//  iHedron
//
//  Created by George Radev on 2011-07-24.
//  Copyright 2011 I Me Mine. All rights reserved.
//

#import "SQLiteCore.h"

@implementation SQLiteCore
//_________________________________________ implementation
+ (void)load{}
+ (void)initialize{}
+ (id)new{	
    return nil;
}
+ (id)allocWithZone:(NSZone *)zone{	
    return nil;
}
+ (id)alloc{	
    return nil;
}

//_________________________________________ statics
static sqlite3* database;

//_________________________________________ static messages
+(void) init{
	char* dbName;
#if TARGET_IPHONE_SIMULATOR
	dbName = "/Users/george/Projects/iProjects/iHedron/iHedron.sqlite";
#else
	{
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDir = [paths objectAtIndex:0];
		dbName = (char*)[[documentsDir stringByAppendingPathComponent:@"iHedron.sqlite"] UTF8String];
	}
#endif
	if(sqlite3_open(dbName, &database) != SQLITE_OK) {
		sqlite3_close(database);
		database = NULL;
	}else {
		//create DB tables if DB is new
		[SQLiteCore queryDDL:"CREATE TABLE IF NOT EXISTS hstock ( sid INTEGER PRIMARY KEY, sname VARCHAR)"];
		[SQLiteCore queryDDL:"CREATE TABLE IF NOT EXISTS horder ( oid INTEGER PRIMARY KEY, odate INTEGER, shipdate INTEGER, ocustomerid INTEGER, ocustomer VARCHAR, ototal DOUBLE)"];
		[SQLiteCore queryDDL:"CREATE TABLE IF NOT EXISTS horderstock ( oid INTEGER, sid INTEGER, qty INTEGER, price DOUBLE)"];
		
		[SQLiteCore queryDDL:"CREATE INDEX IF NOT EXISTS horder_date_idx ON horder (odate)"];
		[SQLiteCore queryDDL:"CREATE INDEX IF NOT EXISTS horder_customer_idx ON horder (ocustomerid)"];
		[SQLiteCore queryDDL:"CREATE INDEX IF NOT EXISTS horderstock_oid_idx ON horderstock (oid)"];
	}
}

+(void) done{
	if(database){
		sqlite3_close(database);
	}
}

+(sqlite3_stmt*) prepare: (char*) sql{
	sqlite3_stmt* statement;
	if(database){
		if(sqlite3_prepare(database, sql, -1, &statement, NULL) != SQLITE_OK){
			NSLog( @"SQLError: %s", sqlite3_errmsg(database));
			sqlite3_finalize(statement);
			statement = NULL;
		}
	}
	return statement;
}

+(BOOL) queryDDL: (char*) sql{
	BOOL result = NO;
	sqlite3_stmt* statement;
	if(database){
		if(sqlite3_prepare(database, sql, -1, &statement, NULL) != SQLITE_OK){
			NSLog(@"SQLError: %s", sqlite3_errmsg(database));
		}else{
			result = YES;
		}
		if(statement){
			result = (SQLITE_DONE == sqlite3_step(statement));
			sqlite3_finalize(statement);
			statement = NULL;
		}
	}
	return result;
}

+(BOOL) ddl: (sqlite3_stmt*) statement{
	if(database && statement){
		int result = sqlite3_step(statement); 
		if(result == SQLITE_DONE){
			return YES;
		}
	}
	NSLog(@"SQLError: %s", sqlite3_errmsg(database));
	return NO;
}

+(BOOL) next: (sqlite3_stmt*) statement{
	if(!database ||  !statement){
		return NO;
	}	
	int result = sqlite3_step(statement); 
	if(result == SQLITE_ROW ){
		return YES;
	}
	if(result != SQLITE_ROW && result != SQLITE_DONE && result != SQLITE_OK){
		NSLog(@"SQLError: %s", sqlite3_errmsg(database));
	}
	return NO;
}

+(void) close: (sqlite3_stmt*) statement{
	if(statement){
		sqlite3_finalize(statement);
		statement = NULL;
	}
}

+(BOOL) beginTransaction{
	return [SQLiteCore queryDDL:"BEGIN"];
}

+(BOOL) commit{
	return [SQLiteCore queryDDL:"COMMIT"];
}

+(BOOL) rollback{
	return [SQLiteCore queryDDL:"ROLLBACK"];
}

+(long) lastROWID{
	if(database){
		return sqlite3_last_insert_rowid(database);
	} else {
		return 0;
	}
}

+(NSString*) getString: (sqlite3_stmt*) statement index:(int) i{
	if(statement){
		char * sname = (char*)sqlite3_column_text(statement, i);
		NSString * result;
		if (sname == nil) {
			result = [NSString stringWithString: @""];
		}else{
			result = [NSString stringWithUTF8String:sname];
			return [[result retain] autorelease];
		}
		return result;
	}else{
		return @"";
	}
}

@end
